package com.ruanjian.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanjian.reggie.common.R;
import com.ruanjian.reggie.entity.Employee;
import com.ruanjian.reggie.mapper.EmployeeMapper;
import com.ruanjian.reggie.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Employee emp = employeeService.getOne(new QueryWrapper<Employee>().eq("username", employee.getUsername()));
        if(emp == null){
            return R.error("登录失败");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }if(emp.getStatus() == 0){
            return R.error("账号已经禁用");
        }request.getSession().setAttribute("employee",emp.getId());
        System.out.println(request);
        return R.success(emp);
    }

    /**
     * 管理员退出操作
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
//        request.getSession().setAttribute("employee",null);这是我写的的操作
        request.getSession().removeAttribute("employee");
        System.out.println(request.getSession());
        return R.success("退出成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page pageInfo = new Page(page,pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        /**
         * 这里没用使用视频中使用的“Lambda表达式使用了自己平时常用的一种
         * 这里如果直接还是用name.isEmpty()会报空指针异常
         * */
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("update_time");
        employeeService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }

    /**
     * 员工添加功能
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工，员工信息为"+employee.toString());
       //设置初始密码并且进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 根据id来修改运功信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        /*
        * 这里有一个问题加上HttpServletRequest 在实现账号状态修改时不报错但是删掉就会报错
        * 回头查看线程那里
        * */
//        Long employeeId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(employeeId);
        employeeService.updateById(employee);
        return R.success("员工修改成功");
    }


    /**
     * 根据员工来查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("未查询到员工");
    }




}
